<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 文件上传下载 模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class up_down_model extends CI_Model {
	function __construct() {
		parent::__construct ();
		
	}
	public function upLoadIcon($type='file',$whereArray,$table) {
		
		$jsonArray 	= 	array ();
		$array		=	array("inputName"=>$type ,"allowType" => "","allowSize"=>"","saveDir"=>"","isRename" =>true);
		$path 		= 	$_POST ['path'];
		$this->load->library ( 'images',$array);
		//$images = $this->images;
		$res 		= 	$this->images->thumb ( $path, false, 1 );
		if ($res == false) {
			echo "裁剪失败";
		} elseif (is_array ( $res )) {
			$pre						= end(explode("_",$table));
			$updateData[$pre."Icon"]	= cut_str($res['small'], base_document('/QDCI/'));
			$updateData[$pre."BigIcon"]	= cut_str($res['big'], base_document('/QDCI/'));
			$code=$this->updateUserInfo($whereArray, $updateData, $table);
			$jsonArray = array_merge ( $res, array ('type' => 1 ) );
		} elseif (is_string ( $res )) {
			$pre						= end(explode("_",$table));
			$updateData[$pre."Icon"]	= cut_str($res,base_document('/QDCI/'));
			$code=$this->updateUserInfo($whereArray, $updateData, $table);
			$jsonArray = array ('image' => $res, 'type' => 0 );
		}
		$this->session->set_userdata($updateData);
		echo json_encode ( $jsonArray );
	}
	public function upLoadImage($fileName,$type='default') {
		$array=array("inputName"=>$fileName ,"type"=>$type,"allowType" => "","allowSize"=>"","saveDir"=>"","isRename" =>true);
		log_message("info",json_encode($_FILES));
		$this->load->library ( 'images',$array);
		$path = $this->images->move_uploaded ();
		//$images->thumb ( $path['path'], false, 0 ); //文件比规定的尺寸大则生成缩略图，小则保持原样
		
		if ($path == false) {
			return array('type'=>0,'path'=>'系统繁忙，请稍后再试');
		} else {
			$path['localPath']=base_document('/QDCI/'.$path['path']);
			$path['path']=base_url($path['path']);
			$path['type']=1;
			return $path;
		}
	}
	private function  updateUserInfo($whereArray,$upateData,$table){
		$this->db->where($whereArray);
		$this->db->update($table,$upateData);
		return $this->db->affects_row();
	}
}